<?php
namespace AlbumTest\Controller;

use PHPUnit_Framework_TestCase;
use AlbumTest\Bootstrap;
use Album\Controller\AlbumController;
use Zend\Http\Request;
use Zend\mvc\Router\Http\TreeRouteStack as HttpRouter;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\MvcEvent;

class AlbumControllerTest extends PHPUnit_Framework_TestCase
{
	protected $controller;
	protected $request;
	protected $response;
	protected $routeMatch;
	protected $event;
	
	protected function setUp()
	{
		$serviceManager = Bootstrap::getServiceManager();
		$this->controller = new AlbumController();
		$this->request = new Request();
		$this->routeMatch = new RouteMatch(array('controller' => 'index'));
		$this->event = new MvcEvent();
		$config = $serviceManager->get("Config");
		$routerConfig = isset($config['router']) ? $config['router'] : array();
		$router = HttpRouter::factory($routerConfig);
		$this->event->setRouter($router);
		$this->event->setRouteMatch($this->routeMatch);
		$this->controller->setEvent($this->event);
		$this->controller->setServiceLocator($serviceManager);
	}
	
	public function testAddActionCanBeAccessed()
	{
		$this->routeMatch->setParam('action', 'add');
		 
		
		$result = $this->controller->dispatch($this->request);
		$response = $this->controller->getResponse();
		$this->assertEquals(200, $response->getStatusCode());
	}
	
	public function testDeleteActionCanBeAccessed()
	{
		$this->routeMatch->setParam('action', "delete");
		$this->routeMatch->setParam('id', 1);
		$result = $this->controller->dispatch($this->request);
		$response = $this->controller->getResponse();
		$this->assertEquals(200, $response->getStatusCode());
	}
	
	public function testEditActionCanBeAccessed()
	{
		$this->routeMatch->setParam('action', 'edit');

		//添加
		$this->routeMatch->setParam('id', 1);
		$result = $this->controller->dispatch($this->request);
		$response = $this->controller->getResponse();
		$this->assertEquals(200, $response->getStatusCode());
	}
	
	public function testIndexActionCanBeAccessed()
	{
		$this->routeMatch->setParam('action', 'index');
		$result = $this->controller->dispatch($this->request);
		$response = $this->controller->getResponse();
		$this->assertEquals(200, $response->getStatusCode());
	}
	
	public function testGetAlbumTableReturnsAnInstanceOfAlbumTable()
	{
		$this->assertInstanceOf('Album\Model\AlbumTable', $this->controller->getAlbumTable());
	}
	
	public function testEditActionRedirect()
	{
		$this->routeMatch->setParam('action', 'edit');
		$result = $this->controller->dispatch($this->request);
		$response = $this->controller->getResponse();
		$this->assertEquals(302, $response->getStatusCode());
	}
	
	public function testDeleteActionRedirect()
	{
		$this->routeMatch->setParam('action', 'delete');
		$result = $this->controller->dispatch($this->request);
		$response = $this->controller->getResponse();
		
		$this->assertEquals(302, $response->getStatusCode());
	}
}


?>